/*
 * ThreadExecuter.h
 *
 *  Created on: 2014. 6. 19.
 *      Author: jjaehuny
 */

#ifndef FRAMEWORK_UTIL_THREADEXECUTER_H_
#define FRAMEWORK_UTIL_THREADEXECUTER_H_

#include <functional>
#include <vector>
#include <thread>

#include <glog/logging.h>

#include "../Type.h"

namespace Framework { namespace Util {

class ThreadExecuter
{
public:
	ThreadExecuter() {}
	~ThreadExecuter()
	{
		LOG(INFO) << __PRETTY_FUNCTION__;

		DetachAll();
		thread_vector_.clear();
	}

public:
	void Run(UINT max_count, std::function<void()> function)
	{
		for (UINT count = 0; count < max_count; count++)
		{
			thread_vector_.push_back(std::thread(function));
		}
	}

	void DetachAll()
	{
		for (auto & thread : thread_vector_)
		{
			thread.join();
		}
	}

private:
	std::vector<std::thread> thread_vector_;
};

} /*Util*/ } /*Framework*/

#endif /* FRAMEWORK_UTIL_THREADEXECUTER_H_ */
